<?php
class Proveedores extends CI_Controller {
    public function __construct() {
        parent::__construct();
        parent::__construct();
        $this->load->library(
                array(
                    'session',
                    'r_session',
                    'form_validation'
                ));
        $this->load->helper(
                array(
                    'url'
                ));
        $this->load->model(
                array(
                    'proveedores_model'
                ));
        $this->r_session->check($this->session->all_userdata());
    }
    
    public function index() {
        $session = $this->session->all_userdata();
        $session['title'] = 'Listado de Proveedores';
        $data['proveedores'] = $this->proveedores_model->get_proveedores();
        
        $this->load->view('layout/header', $session);
        $this->load->view('proveedores/index', $data);
        $this->load->view('layout/footer');
    }
    
    public function agregar() {
        $session = $this->session->all_userdata();
        $session['title'] = 'Agregar de Proveedor';
        $data = array();
        
        $this->form_validation->set_rules('proveedor', 'Proveedor', 'required');
        
        if($this->form_validation->run() == FALSE) {
            
        } else {
            if($this->proveedores_model->existe_proveedor_por_nombre($this->input->post('proveedor'))) {
                $data['existe'] = 'El proveedor '.$this->input->post('proveedor').' ya existe';
            } else {
                $datos = array(
                    'nombre' => $this->input->post('proveedor')
                );
                
                $visa = $this->input->post('visa');
                if($visa == 1) {
                    $datos['visa'] = 1;
                } else {
                    $datos['visa'] = 0;
                }
                
                $this->proveedores_model->set_proveedor($datos);
                
                redirect('/proveedores/', 'refresh');
            }
        }
        
        $this->load->view('layout/header', $session);
        $this->load->view('proveedores/agregar', $data);
        $this->load->view('layout/footer');
    }
    
    public function update($id) {
        $session = $this->session->all_userdata();
        $session['title'] = 'Modificar Proveedor';
        $data['proveedor'] = $this->proveedores_model->get_proveedor_por_id($id);
        
        $this->form_validation->set_rules('proveedor', 'Proveedor', 'required');
        
        if($this->form_validation->run() == FALSE) {
            
        } else {
            $datos = array(
                'nombre' => $this->input->post('proveedor')
            );
            $visa = $this->input->post('visa');
            if($visa == 1) {
                $datos['visa'] = 1;
            }
            
            $this->proveedores_model->update($datos, $id);
            
            redirect('/proveedores/', 'refresh');
        }
        
        $this->load->view('layout/header', $session);
        $this->load->view('proveedores/update', $data);
        $this->load->view('layout/footer');
    }
}
?>
